<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller 
{
	function index()
	{
		$user=$this->session->userdata('user_info');
	    if($user->username!='root')
	    {
	    	redirect(base_url());
	    }
	    $this->load->model('common');
	    $data['data']=$this->common->Get('users');
	    $this->load->view('common/header');
	    $this->load->view('user/index',($data));
	    $this->load->view('common/footer');
	}
	function add($param=null)
	{
		$user=$this->session->userdata('user_info');
	    if($user->username!='root')
	    {
	    	redirect(base_url());
	    }
		$this->load->model('common');
		if($this->input->post('save')!=null)
		{
			$data=array(
				'username'=>$this->input->post('user'),
				'password'=>$this->input->post('pass')
			);
			$rs=$this->common->GetById('users',array('username'=>$this->input->post('user')));
			if(!$rs)
			{
			    $this->common->update('users',$data);
			    $id=$this->db->insert_id();
			    $this->update_user_api($id);
			    redirect(base_url().'index.php/user');
			}
			else 
			{
			    $data['error']='User name "'.$this->input->post('user').'" is exist';
			}
			
		}
		$rs=$this->common->Get('api');
		//print_r($rs);
		$data['apis']=$rs;//json_decode($rs);
		$this->load->view('common/header');
		$this->load->view('user/form',($data));
		$this->load->view('common/footer');	
	}
	function change_pass($param=null)
	{
	$user=$this->session->userdata('user_info');
	    if($user->username!='root')
	    {
	    	redirect(base_url());
	    }
		if($param!=null)
		{
			$this->load->model('common');
			$rs=$this->common->GetById('users',array('id'=>$param));
			if($rs)
			{
				$data=array('password'=>$this->input->post('pass'));
				$this->common->update('users',$data,0,array('id'=>$param));
				//redirect(base_url().'index.php/user');
			}
			//redirect(base_url().'index.php/user');
		}
		redirect(base_url().'index.php/user');
	}
	function update_api($param=null)
	{
		$user=$this->session->userdata('user_info');
	    if($user->username!='root')
	    {
	    	redirect(base_url());
	    }
		if($param!=null)
		{
			$this->load->model('common');
			$rs=$this->common->GetById('users',array('id'=>$param));
			if($rs)
			{
				if($rs[0]->username!='root')
				{
					$this->update_user_api($param);
				}
				//redirect(base_url().'index.php/user');
			}
			//redirect(base_url().'index.php/user');
		}
		redirect(base_url().'index.php/user');
	}
	function delete($param=null)
	{
		if($param!=null)
		{
			$this->load->model('common');
			$rs=$this->common->GetById('users',array('id'=>$param));
			if($rs)
			{
				if($rs[0]->username!='root')
				{
					$this->common->delete('users',array('id'=>$param));
				}
				//redirect(base_url().'index.php/user');
			}
			//redirect(base_url().'index.php/user');
		}
		redirect(base_url().'index.php/user');
	}
	function update_user_api($user_id)
	{
		$this->load->model('common');
		$this->common->delete('users_api',array('user_id'=>$user_id));
		foreach ($this->input->post('api') as $item)
		{
		    echo $item.'<br>';
			$data=array('user_id'=>$user_id,'api_id'=>$item);
			$this->common->update('users_api',$data);
		}
	}
	
}